<template>
    <view>
        <!-- 搜索框 -->
         <view class="search-box">
              <my-search @click="gotoSearch"></my-search>
         </view>
        <!-- 轮播图部分 -->
        <swiper :indicator-dots="true" :autoplay="true" :interval="3000" :duration="1000">
            <swiper-item v-for="(item,i) in swiperList" :key="item.goods_id">
                <navigator :url="`/subpkg/goods_detail/goods_detail?goods_id=${item.goods_id}`" class="swiper-item">
                    <image :src="item.image_src" mode=""></image>
                </navigator >
            </swiper-item>
        </swiper>
        <!-- 导航部分 -->
        <view class="nav-list">
            <view class="nav-item" v-for="(item,i) in navList" :key="i" @click="navClickHandler(item)">
                <image :src="item.image_src" mode=""></image>
            </view>
        </view>
        <!-- 楼层部分 -->
        <view class="floor-list">
            <view class="floor-item" v-for="(item,i) in floorList" :key="i">
                <!-- 楼层title -->
                <image class="floor-title" :src="item.floor_title.image_src" mode=""></image>
                <!-- 楼层图片 -->
                <view class="floor-img-box">
                    <!-- 左侧大图片 -->
                    <navigator class="left-img-box" :url="item.product_list[0].url">
                        <image :style="{width:item.product_list[0].image_width+'rpx'}" :src="item.product_list[0].image_src" mode="widthFix"></image>
                    </navigator>
                    <!-- 右侧4张小图片 -->
                    <view class="right-img-box">
                        <navigator class="right-img-item" v-for="(item2,i2) in item.product_list" :key="i2" v-if="i2>0" :url="item2.url">
                            <image :src="item2.image_src" :style="{width:item2.image_width+'rpx'}" mode="widthFix"></image>
                        </navigator>
                    </view>
                </view>
            </view>
        </view>
    </view>
</template>

<script>
   // 导入自己封装的 mixin 模块
   import badgeMix from '@/mixins/setBadge.js'
   export default {
       // 将 badgeMix 混入到当前的页面中进行使用
         mixins: [badgeMix],
        data() {
            return {
                // 轮播图的数据
                swiperList:[],
                // 导航数据
                navList:[],
                // 楼层数据
                floorList:[]
            };
        },
        onLoad() {
            this.getSwiperList()
            this.getNavList()
            this.getFloorList()
        },
        methods:{
            gotoSearch() {
              uni.navigateTo({
                url: '/subpkg/search/search'
              })
            },
         async   getSwiperList(){
                let {data:res} = await uni.$http.get('/home/swiperdata')
                // console.log('res',res);
                // 处理请求失败的问题
                // if(res.meta.status !== 200) {
                //     return uni,showToast({
                //         title:'数据请求失败!',
                //         duration:1500,
                //         icon:'none',
                //     })
                // }
                if(res.meta.status !== 200) return uni.$showMsg()
                // 请求成功
                this.swiperList = res.message
            },
         async   getNavList(){
                let {data:res} = await uni.$http.get('/home/catitems')
                // console.log(res);
                if(res.meta.status !== 200) return uni.$showMsg()
                this.navList=res.message
            },
           async getFloorList(){
                let {data:res} = await uni.$http.get('/home/floordata')
                // console.log(res);
                 if(res.meta.status !== 200) return uni.$showMsg()
                 // 设置页面数据之前,做数据转换
                 res.message.forEach(floor => {
                   floor.product_list.forEach(prod => {
                     prod.url = '/subpkg/goods_list/goods_list?' + prod.navigator_url.split('?')[1]
                     // prod.url = `/subpkg/goods_list?${prod.navigator_url.split('?')[1]}` // 模版字符串写法
                   })
                 })
                 this.floorList=res.message
            },
            // 事件处理函数,跳转到分类页面
            navClickHandler(item){
                if(item.name === "分类") {
                    uni.switchTab({
                          url: '/pages/cate/cate'
                          //url:"../cate/cate" //或者是使用相对路径
                        })
                }
            }
        }
    }
</script>

<style lang="scss">
    .search-box {
        // 吸顶效果
        position: sticky;
        top:0;
        // 提高层级
         z-index: 9;
    }
swiper {
    height: 330rpx;
    
    .swiper-item,image {
        width: 100%;
        height: 100%;
    }
}
.nav-list {
    display: flex;
    justify-content: space-evenly;
    margin: 32rpx 0;    
    .nav-item {
        image {
            width: 128rpx;
            height: 140rpx;
        }
    }
}
.floor-title {
  height: 60rpx;
  width: 100%;
}
.floor-img-box {
    display: flex;
    padding-left: 10rpx;
    
    .right-img-box {
        display: flex;
        flex-wrap: wrap;
        justify-content: space-around;
    }
}
</style>
